package com.shop.om.conf.shiro;

import com.jt.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * @author cjt
 * @version 1.0.0
 * @description
 * @date 2020/6/28  10:29
 * @Version v1.0
 **/
@Slf4j
public class ShiroSessionManager  extends DefaultWebSessionManager {
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        String id = WebUtils.toHttp(request).getHeader("HEADER_TOKEN_NAME");
        if (StringUtils.isBlank(id))  {
            //如果没有携带id参数则按照默认方式获取,事实上,只有登录接口和一些不需要游客身份使用的接口以外,没有任何接口会走这个逻辑，因为我们已经没有了cookie
            //  System.out.println("super："+super.getSessionId(request, response));
            return super.getSessionId(request, response);
        }else{
         request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE,"stateless");
         request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID,id);
         request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID,Boolean.TRUE);
         return id;
        }
    }
}
